﻿using System;
using BingWallpaper.Properties;
using System.Windows.Forms;
using Microsoft.Win32;

namespace BingWallpaper
{
    internal class SettingClass
    {
        public static void SaveSettings(CheckBox sender)
        {
            switch (sender.Name)
            {
                case "cbBoot":
                    Settings.Default.Boot = sender.Checked;
                    SetBoot();
                    break;
                case "cbAutoSet":
                    Settings.Default.AutoSet = sender.Checked;
                    break;
                case "cbImgSize_1080":
                    Settings.Default.ImgSize_1920x1080 = sender.Checked;
                    break;
            }
            Settings.Default.Save();
        }

        public static void SetBoot()
        {
            try
            {
                var run = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                var temp = run?.GetValue("BingWallpaper");
                if (temp == null || (string)temp != Application.ExecutablePath)
                {
                    if (Settings.Default.Boot)
                    {
                        run?.SetValue("BingWallpaper", Application.ExecutablePath);
                    }
                }
                else
                {
                    if (!Settings.Default.Boot)
                    {
                        run.DeleteValue("BingWallpaper");
                    }
                }
                run?.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, @"提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public static bool GetAutoSet() => Settings.Default.AutoSet;

        public static bool GetBoot() => Settings.Default.Boot;

        public static bool GetImgSize_1080() => Settings.Default.ImgSize_1920x1080;
    }
}
